import React, {useState} from "react";
import {Menu, MenuProps} from "antd";
import {routerItem} from "@/router/router";
import {useLocation, useNavigate} from "react-router-dom";

/**
 * 左侧菜单
 * @constructor
 */
const NavLeft = () => {
    const [router]: any = useState(routerItem)
    const navigate = useNavigate();
    const location= useLocation();
    //只适用二级菜单
    const pathList= location.pathname.split('/')
    const path='/'+pathList[1]+'/'+pathList[2]
    const MenuItem: MenuProps['items'] = router[2].children.map((e: { path: string; name: string; children: Array<object> }) => {
        return {
            key: e.path,
            label: e.name,
            children: e.children ?
                e.children.map((e1:any) => {
                    return {
                        key: e1.path,
                        label: e1.name,
                    }
                }) : ''
        }
    })
    console.log(MenuItem)

    return (
        <>
            <Menu
                mode="inline"
                defaultSelectedKeys={[location.pathname]}
                defaultOpenKeys={[path]}
                style={{height: '100%', borderRight: 0}}
                items={MenuItem}
                onSelect={(e) => {
                    console.log(e);
                    navigate(e.key)
                }}
            />
        </>

    )
}
export default NavLeft;
